package com.hoshiibuy.purchase.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.purchase.entity.Po;
import com.hoshiibuy.purchase.entity.param.PoQueryParam;
import com.hoshiibuy.purchase.entity.param.PoUpdateParam;
import com.hoshiibuy.purchase.entity.param.PoAddParam;
import com.hoshiibuy.purchase.service.PoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  控制器
 * </p>
 *
 * @author campchen
 * @since 2019-05-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/po")
@Api(value = "采购", tags = {"采购单管理"})
public class PoController {
    @Autowired
    private PoService poService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询
    */
    @ApiOperation(value = "分页查询")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PoQueryParam param) {
        try {
            log.info("分页查询");
            QueryWrapper<Po> queryWrapper = new QueryWrapper<Po>();
            queryWrapper.eq(param.getCompanyid() != null, "companyid", param.getCompanyid());
            queryWrapper.eq(param.getPlatformid() != null, "platformid", param.getPlatformid());
            queryWrapper.eq(param.getStoreid() != null, "storeid", param.getStoreid());
            queryWrapper.eq(param.getSource() != null, "source", param.getSource());
            queryWrapper.eq(param.getType() != null, "type", param.getType());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPoNo()), "po_no", param.getPoNo());
            queryWrapper.eq(param.getPoDate() != null, "po_date", param.getPoDate());
            queryWrapper.eq(param.getVendorId() != null, "vendor_id", param.getVendorId());
            queryWrapper.eq(param.getVendorWhid() != null, "vendor_whid", param.getVendorWhid());
            queryWrapper.eq(param.getPlatformWhid() != null, "platform_whid", param.getPlatformWhid());
            queryWrapper.eq(param.getStoreWhid() != null, "store_whid", param.getStoreWhid());
            queryWrapper.eq(param.getOrderAmount() != null, "order_amount", param.getOrderAmount());
            queryWrapper.eq(param.getOrderCount() != null, "order_count", param.getOrderCount());
            queryWrapper.eq(param.getMemberid() != null, "memberid", param.getMemberid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getAddress()), "address", param.getAddress());
            queryWrapper.eq(StringUtils.isNotBlank(param.getConsignee()), "consignee", param.getConsignee());
            queryWrapper.eq(param.getCountry() != null, "country", param.getCountry());
            queryWrapper.eq(param.getProvince() != null, "province", param.getProvince());
            queryWrapper.eq(param.getCity() != null, "city", param.getCity());
            queryWrapper.eq(param.getDistrict() != null, "district", param.getDistrict());
            queryWrapper.eq(StringUtils.isNotBlank(param.getZipcode()), "zipcode", param.getZipcode());
            queryWrapper.eq(StringUtils.isNotBlank(param.getTelphone()), "telphone", param.getTelphone());
            queryWrapper.eq(StringUtils.isNotBlank(param.getMobile()), "mobile", param.getMobile());
            queryWrapper.eq(StringUtils.isNotBlank(param.getEmail()), "email", param.getEmail());
            queryWrapper.eq(param.getDeliveryDate() != null, "delivery_date", param.getDeliveryDate());
            queryWrapper.eq(param.getStatus() != null, "status", param.getStatus());
            queryWrapper.eq(param.getSettleStatus() != null, "settle_status", param.getSettleStatus());
            queryWrapper.eq(param.getSettleAmount() != null, "settle_amount", param.getSettleAmount());
            IPage<Po> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(Po.class));
            return Response.data(poService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询异常", e);
            return Response.failed("分页查询异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(poService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public Response save(@RequestBody @Valid PoAddParam param) {
        try {
            Po data = BeanUtils.covertType(param, Po.class);
            return Response.data(poService.save(data));
        } catch (ServiceException e) {
            log.error("新增异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public Response update(@RequestBody @Valid PoUpdateParam param) {
        try {
            Po data = BeanUtils.covertType(param, Po.class);
            return Response.data(poService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改异常", e);
            return Response.failed();
        }
    }

    /**
     * @param id
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 删除
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        try {
            Po data=poService.getById(id);
            if(data==null){
                log.error("删除异常，无法找到记录");
                return Response.failed("删除异常，无法找到记录");
            }
            poService.deleteById(data);
            return Response.success("删除成功");
        }catch (ServiceException e) {
            log.error("删除异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("删除异常", e);
            return Response.failed();
        }
    }

}
